View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  
22  package org.universAAL.ri.gateway.communicator;
23  
24  import org.osgi.framework.BundleActivator;
25  import org.osgi.framework.BundleContext;
26  import org.universAAL.middleware.container.ModuleContext;
27  import org.universAAL.middleware.container.osgi.uAALBundleContainer;
28  import org.universAAL.middleware.tracker.IBusMemberRegistry;
29  import org.universAAL.middleware.tracker.IBusMemberRegistryListener;
30  import org.universAAL.ri.gateway.communicator.service.impl.CommunicatorStarter;
31  import org.universAAL.ri.gateway.eimanager.ExportOperationInterceptor;
32  import org.universAAL.ri.gateway.eimanager.ImportOperationInterceptor;
33  import org.universAAL.ri.gateway.eimanager.impl.EIManagerRegistryListener;
34  import org.universAAL.ri.gateway.eimanager.impl.EIOperationManager;
35  import org.universAAL.ri.gateway.eimanager.impl.ExportManagerImpl;
36  import org.universAAL.ri.gateway.eimanager.impl.ImportManagerImpl;
37  import org.universAAL.ri.gateway.eimanager.impl.security.ExportSecurityOperationInterceptor;
38  import org.universAAL.ri.gateway.eimanager.impl.security.ImportSecurityOperationInterceptor;
39  
40  /**
41   * Bundle's activator. Starts a default instance of the GatewayCommunicator
42   * worker by use of GatewayCommunicatorInstantiator.
43   * 
44   * @author skallz
45   * 
46   */
47  public class Activator implements BundleActivator {
48  
49      /**
50       * the communicator's starter.
51       */
52      private CommunicatorStarter inst;
53      public static BundleContext bc;
54      public static ModuleContext mc;
55  
56      private IBusMemberRegistry registry;
57  
58      private IBusMemberRegistryListener exportRegistryListener;
59      private IBusMemberRegistryListener importRegistryListener;
60  
61      private ExportManagerImpl exportManager;
62      private ImportManagerImpl importManager;
63      /**
64       * Starts the communicator.
65       * 
66       * @param context
67       *            bundle context
68       */
69      public void start(final BundleContext context) throws Exception {
70  	bc = context;
71  	
72  	mc = uAALBundleContainer.THE_CONTAINER
73  		.registerModule(new Object[] { context });
74  	
75  	inst = new CommunicatorStarter(context);
76  
77  	exportManager = new ExportManagerImpl(inst.getCommunicator());
78  	importManager = new ImportManagerImpl(inst.getCommunicator());
79  	
80  	inst.setManagers(importManager, exportManager);
81  
82  	registry = (IBusMemberRegistry) mc.getContainer().fetchSharedObject(mc,
83  		IBusMemberRegistry.busRegistryShareParams);
84  
85  	exportRegistryListener = new EIManagerRegistryListener(exportManager);
86  	
87  	importRegistryListener = new EIManagerRegistryListener(importManager);
88  	
89  	registry.addBusRegistryListener(exportRegistryListener, true);
90  	registry.addBusRegistryListener(importRegistryListener, true);
91  	
92  	EIOperationManager.Instance.init();
93  	
94  	Activator.mc.getContainer().shareObject(Activator.mc, new ImportSecurityOperationInterceptor(),
95  			new Object[] { ImportOperationInterceptor.class.getName() });
96  	
97  	Activator.mc.getContainer().shareObject(Activator.mc, new ExportSecurityOperationInterceptor(),
98  			new Object[] { ExportOperationInterceptor.class.getName() });
99      
100     }
101 
102     /**
103      * Stops the communicator and all used resources.
104      * 
105      * @param context
106      *            bundle context
107      */
108     public void stop(final BundleContext context) {
109 	inst.stop();
110 	if (registry!= null){
111 	    registry.removeBusRegistryListener(exportRegistryListener);
112 	    registry.removeBusRegistryListener(importRegistryListener);
113 	}
114 	
115 	if (exportManager != null){
116 	    exportManager.shutdown();
117 	}
118     }
119 
120 }